<%@page import="java.util.Iterator"%>
<%@page import="com.gkwave.module.bean.BranchBean"%>
<%@page import="com.gkwave.module.bean.LocationBean"%>
<%@page import="com.gkwave.module.dao.HealthCareDAO"%>
<%@page import="com.gkwave.module.bean.FoodMenuBean"%>
<%@page import="java.util.List"%>
<%@page import="com.gkwave.module.dao.FoodMenuDAO"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>::FoodMenu::</title>
<script type="text/javascript">
	function submitPage(arg){
		document.forms["foodmenu"].hidden_1.value=arg;
		document.forms["foodmenu"].submit();
	}
</script>
<%
	String action = (String) request.getParameter("hidden_1");
	String message = null;
	
	HealthCareDAO dao = new HealthCareDAO();
	List<LocationBean> list = dao.getLocationList();
	List<BranchBean> branchList = null;
	String idCity = (String)request.getParameter("idCity");
	System.out.println(action);
	Integer id_city = 0;
/* 	if(action!= null && action.equals("save")){
		idCity = null;
	}else{
		if(idCity!=null){
			id_city = Integer.parseInt(idCity);
			branchList = dao.getBranchesByLocation(id_city);
		}	
	} */	
	
	
	boolean isSubmitted = false;
	boolean result = false;
	boolean finalSubmit = false;
	FoodMenuDAO dao1 = new FoodMenuDAO();
	String[] idFoodMenus= request.getParameterValues("idFoodMenu");
	String strCity = request.getParameter("idCity");
	String strBranch = request.getParameter("idBranch");
	
	if (action != null && action.equals("savefoodmenu")) {
		isSubmitted = true;
		String nmFoodMenu = (String) request.getParameter("nmFoodMenu");
		String txtFoodMenu = (String) request.getParameter("txtFoodMenu");
		
		result = dao1.saveFoodMenu(nmFoodMenu, txtFoodMenu);
		if(result){
			message = "Data Saved Successfully!!!";
		} else{
			message = "Error!!!";
		}
	}else if(action!=null && action.equals("linkfoodmenu")) {

		for(int i=0;i<idFoodMenus.length;i++){
			String strFood = idFoodMenus[i];
//			boolean value = dao1.linkFoodMenu(strBranch,strFood);
			isSubmitted = true;
			result = true;
			finalSubmit = true;
		}
	}else if(idCity!=null){
		id_city = Integer.parseInt(idCity);
		branchList = dao.getBranchesByLocation(id_city);
	}
	List<FoodMenuBean> foodmenubean = dao1.getFoodMenuList();
	
	if(finalSubmit){
		String strLoc = null;
		String strBra = null;
		if(list!=null){
			Iterator ite = list.iterator();
			while(ite.hasNext()){
				LocationBean locationbean = (LocationBean)ite.next();
				Integer id = locationbean.getIdLocation();
				if(id.equals(Integer.parseInt(strCity))){
					strLoc = locationbean.getNmLocation();
					break;
				}
			}
		}
		branchList = dao.getBranchesByLocation(Integer.parseInt(strCity));
		System.out.println("::: " + branchList);
		if(branchList!=null){
			Iterator ite = branchList.iterator();
			while(ite.hasNext()){
				BranchBean branchbean = (BranchBean)ite.next();
				Integer id = branchbean.getIdBranch();
				if(id.equals(Integer.parseInt(strBranch))){
					strBra = branchbean.getNmBranch();
					break;
				}
			}
		}
		String[] fmenuArray = new String[0];
		if(foodmenubean!=null){
			Iterator ite = foodmenubean.iterator();
			fmenuArray = new String[idFoodMenus.length];
			for(int i=0;i<idFoodMenus.length;i++){
				while(ite.hasNext()){
					FoodMenuBean fmenubean = (FoodMenuBean)ite.next();
					Integer id = fmenubean.getIdFoodMenuDetail();
					if(id.equals(Integer.parseInt(idFoodMenus[i]))){
						fmenuArray[i] = fmenubean.getNmFoodMenu();
						break;
					}
					
				}
			}
		}		
		dao1.updateNotification(strLoc, strBra, fmenuArray);
	}
%>
</head>

<body>
<form name="foodmenu" action="foodmenu" method="post">
<input type="hidden" name="hidden_1">
	<table>
	<tr><td>
	<table>
		<thead>Create Food Menu</thead>
		<%
		if(isSubmitted){
			if(result){
		%>
			<tr align="center">
				<td align="center" colspan="2">Data saved successfully!!!</td>
			</tr>
		<%
			}else{
		%>
			<tr align="center">
				<td align="center" colspan="2">Error...!!!</td>
			</tr>
		<%
			}
		}
		%>
		<tr>
			<td>Food Menu Name</td><td><input type="text" name="nmFoodMenu"></td>
		</tr>
		<tr>	
			<td valign="top">Description</td><td><textarea name="txtFoodMenu" rows="10" cols="35"></textarea></td>
		</tr>
		<tr>
			<td><input type="submit" onclick="submitPage('savefoodmenu')" value="Save"></td><td><input type="reset" value="Reset"></td>
		</tr>
	</table>
	</td>
	<td>
		<table>
			<tr>	
				<td>City</td>
				<td>
					<select name="idCity" onchange="submitPage('refresh')">
						<option value="0">--Select--</option>
					<%
						if(list!=null){
							Iterator ite = list.iterator();
							while(ite.hasNext()){
							LocationBean locationbean = (LocationBean)ite.next();
							Integer id = locationbean.getIdLocation();
							String name = locationbean.getNmLocation();
					%>
						<option value='<%=id%>' <% if(id==id_city){%>selected<%} %>><%=name%></option>
					<%
							}
						}
					%>
					</select>
				</td>
			</tr>
			<tr>	
				<td>Branch</td>
				<td>
					<select name="idBranch">
						<option>--Select--</option>
					<%
						if(branchList!=null){
							Iterator ite = branchList.iterator();
							while(ite.hasNext()){
							BranchBean branchbean = (BranchBean)ite.next();
							Integer id = branchbean.getIdBranch();
							String name = branchbean.getNmBranch();
					%>
						<option value='<%=id%>'><%=name%></option>
					<%
							}
						}
					%>
					</select>
				</td>
			</tr>
			<tr>	
				<td valign="top">Menu List</td>
				<td>
					<select name="idFoodMenu" multiple="multiple" size="10">
					<%
						if(foodmenubean!=null){
							Iterator ite = foodmenubean.iterator();
							while(ite.hasNext()){
							FoodMenuBean fmlist = (FoodMenuBean)ite.next();
							Integer foodid = fmlist.getIdFoodMenuDetail();
							String foodname = fmlist.getNmFoodMenu();
							String foodtxt = fmlist.getNmFoodMenu();
					%>
						<option value='<%=foodid%>'><%=foodname%></option>
					<%
							}
						}
					%>
					</select>			
				</td>
			</tr>
			<tr>
				<td><input type="submit" onclick="submitPage('linkfoodmenu')" value="Link"></td><td><input type="reset" value="Reset"></td>
			</tr>
		</table>		
	</td>
	</tr>
	<tr>
		<td>
			
		</td>
	</tr>
	</table>
</form>
</body>
</html>